package com.peress.mycrew.rowing.ui;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.peress.mycrew.rowing.R;
import com.peress.mycrew.rowing.data.DataGatherer;
import com.peress.mycrew.rowing.data.PieceDataListener;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public final class BasicDataCollectorFragment extends Fragment implements View.OnClickListener,
        PieceDataListener {

    private static DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss.");
    static {
        TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    private DataGatherer mDataGatherer;
    private Button mCollectButton;

    private TextView mTotalDistanceView;
    private TextView mTotalTimeView;
    private TextView mCurrentPaceView;
    private TextView mAveragePaceView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity parent = getActivity();
        mDataGatherer = new DataGatherer(
                (LocationManager) parent.getSystemService(Context.LOCATION_SERVICE),
                (SensorManager) parent.getSystemService(Context.SENSOR_SERVICE));
        mDataGatherer.setPieceDataListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.raw_data, container, false);
        mCollectButton = (Button) view.findViewById(R.id.gather_action_btn);
        mCollectButton.setOnClickListener(this);

        mTotalDistanceView = (TextView) view.findViewById(R.id.total_distance);
        mTotalTimeView = (TextView) view.findViewById(R.id.total_time);
        mCurrentPaceView = (TextView) view.findViewById(R.id.current_pace);
        mAveragePaceView = (TextView) view.findViewById(R.id.average_pace);

        onTotalDistanceChanged(0);
        onTotalTimeChanged(0);
        onCurrentPaceChanged(0);
        onAveragePaceChanged(0);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.gather_action_btn:
                toggleGatherData();
                break;
        }
    }

    private void toggleGatherData() {
        if (mDataGatherer.isGatheringData()) {
            mDataGatherer.stopDataGathering();
            mCollectButton.setText(R.string.start_data_collection);
        } else {
            mDataGatherer.startDataGathering(DataGatherer.MONITOR_ALL);
            mCollectButton.setText(R.string.stop_data_collection);
        }
    }

    @Override
    public void onGatheringStatusChanged(boolean isGathering) {
        Toast.makeText(getActivity(), "Gathering Status: " + isGathering, Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onTotalDistanceChanged(int meters) {
        mTotalDistanceView.setText(NumberFormat.getNumberInstance().format(meters));
    }

    @Override
    public void onTotalTimeChanged(long millis) {
        mTotalTimeView.setText(TIME_FORMAT.format(new Date(millis)) + (millis % 1000) / 100);
    }

    @Override
    public void onCurrentPaceChanged(float currentPace) {
        if (currentPace == 0) {
            mCurrentPaceView.setText("STOPPED");
            return;
        }
        float split = 500.0f / currentPace;
        int minutes = (int) split / 60;
        int seconds = (int) (split % 60);
        int decimal = (int) (((split % 60) * 10) % 10);
        mCurrentPaceView.setText(String.format("%02d:%02d.%d", minutes, seconds, decimal));
    }

    @Override
    public void onAveragePaceChanged(float averagePace) {
        if (averagePace == 0) {
            mAveragePaceView.setText("STOPPED");
            return;
        }
        float split = 500.0f / averagePace;
        int minutes = (int) split / 60;
        int seconds = (int) (split % 60);
        int decimal = (int) (((split % 60) * 10) % 10);
        mAveragePaceView.setText(String.format("%02d:%02d.%d", minutes, seconds, decimal));
    }
}
